package com.frank.loadinglibrary.common.search;

import com.frank.loadinglibrary.utils.SimpleAnimatorListener;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.app.Context;

public class SearchLoadingComponent extends StackLayout {

    private MagnifyComponent magnifyComponent;

    private int padding = 20;

    String pillarColor = "#0000ff";
    String glassColor = "#00ff00";

    private int duration = 3000;

    public SearchLoadingComponent setDuration(int duration) {
        this.duration = duration;
        return this;
    }

    public SearchLoadingComponent setPillarColor(String pillarColor) {
        this.pillarColor = pillarColor;
        return this;
    }

    public SearchLoadingComponent setGlassColor(String glassColor) {
        this.glassColor = glassColor;
        return this;
    }

    public SearchLoadingComponent(Context context) {
        this(context, null);
    }

    public SearchLoadingComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public SearchLoadingComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initUI();
    }

    private void initUI() {
        magnifyComponent = new MagnifyComponent(getContext());
        setPadding(padding, padding, padding, padding);
        DrawTask drawTask = new DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                magnifyComponent.setPillarColor(pillarColor);
                magnifyComponent.setGlassColor(glassColor);
                addComponent(magnifyComponent, getWidth() * 3 / 4, getHeight() * 3 / 4);

                int distance = (int) (getWidth() - magnifyComponent.getRightEdge() - 2 * padding);

                AnimatorProperty animatorPropertyOne = magnifyComponent.createAnimatorProperty();
                animatorPropertyOne.moveByX(distance)
                        .setDuration((long) (duration / 3.8))
                        .setLoopedCount(0);


                AnimatorProperty animatorPropertyTwo = magnifyComponent.createAnimatorProperty();
                animatorPropertyTwo.moveByX(-distance / 2f)
                        .moveByY(distance)
                        .setDuration((long) (duration * 1.4 / 3.8));


                AnimatorProperty animatorPropertyThree = magnifyComponent.createAnimatorProperty();
                animatorPropertyThree.moveByX(-distance / 2f)
                        .moveByY(-distance)
                        .setDuration((long) (duration * 1.4 / 3.8));

                animatorPropertyOne.setStateChangedListener(new SimpleAnimatorListener() {
                    @Override
                    public void onEnd(Animator animator) {
                        super.onEnd(animator);
                        animatorPropertyTwo.start();
                    }
                }).start();

                animatorPropertyTwo.setStateChangedListener(new SimpleAnimatorListener() {
                    @Override
                    public void onEnd(Animator animator) {
                        super.onEnd(animator);
                        animatorPropertyThree.start();
                    }
                });

                animatorPropertyThree.setStateChangedListener(new SimpleAnimatorListener() {
                    @Override
                    public void onEnd(Animator animator) {
                        super.onEnd(animator);
                        animatorPropertyOne.start();
                    }
                });

            }
        };

        addDrawTask(drawTask);
    }
}
